// @ts-nocheck

import type { MutationTree, ActionTree, GetterTree } from 'vuex'
import type { IRootState } from './index'
import { Actions, Mutations } from '@/enum/system'
import {
  treeData as rightsData,
  dataRights as dataRightsData,
} from '@/pages/admin/system/rights.data'
import { getOrgTree, upsertOrg, deleteOrg } from '@/api/org'
import type { IUpsertOrgParams } from '@/api/org'
import { deleteRoleById, getRoleDetail, upsertRoleMenu, getRoleList } from '@/api/role'
// import { treeData as departmentsData } from '@/pages/admin/system/user.data'

// const presetRoles = [
//   {
//     id: 1,
//     name: '【示范】角色名称',
//     desc: '【示范】角色名称的功能权限说明',
//   },
//   {
//     id: 2,
//     name: '【示范】角色名称3',
//     desc: '【示范】角色名称3的功能权限说明的功能权限说明的功能权限说明',
//   },
// ]

export interface ISystemState {
  departments: unknown[]
  roles: unknown[]
  rights: unknown[]
  dataRights: unknown[]
}

export const state: () => ISystemState = () => ({
  departments: [], // departmentsData, // [],
  roles: [], // presetRoles, // [],
  rights: rightsData, // [],
  dataRights: dataRightsData, // [],
})

export const mutations: MutationTree<ISystemState> = {
  [Mutations.SET_DEPARTMENTS](state, departments) {
    state.departments = departments
  },
  [Mutations.SET_ROLES](state, roles) {
    state.roles = roles
  },
  [Mutations.SET_RIGHTS](state, rights) {
    state.rights = rights
  },
  [Mutations.SET_DATA_RIGHTS](state, dataRights) {
    state.dataRights = dataRights
  },
}

export const actions: ActionTree<ISystemState, IRootState> = {
  [Actions.setDepartments]({ commit }, departments) {
    commit(Mutations.SET_DEPARTMENTS, departments)
  },
  [Actions.setRoles]({ commit }, roles) {
    commit(Mutations.SET_ROLES, roles)
  },
  [Actions.setRights]({ commit }, rights) {
    commit(Mutations.SET_RIGHTS, rights)
  },
  [Actions.setDataRights]({ commit }, dataRights) {
    commit(Mutations.SET_DATA_RIGHTS, dataRights)
  },
  async [Actions.getDepartments]({ dispatch }) {
    const data = await getOrgTree()
    dispatch(Actions.setDepartments, data)
    return data
  },
  async [Actions.upsertDepartment]({ dispatch }, department: IUpsertOrgParams) {
    await upsertOrg(department)
    return await dispatch(Actions.getDepartments)
  },
  async [Actions.deleteDepartment]({ dispatch }, id: string | number) {
    await deleteOrg(id)
    return await dispatch(Actions.getDepartments)
  },
  async [Actions.getRoles]({ dispatch }) {
    const data = await getRoleList()
    dispatch(Actions.setRoles, data)
    return data
  },
  async [Actions.upsertRole]({ dispatch }, payload) {
    await upsertRoleMenu(payload)
    return await dispatch(Actions.getRoles)
  },
  async [Actions.deleteRole]({ dispatch }, id: string | number) {
    // console.log(`[Actions.deleteRole]:${id}`)
    await deleteRoleById(id)
    return await dispatch(Actions.getRoles)
  },
}

function flatTree<T = any>(item: Record<string, unknown>, level: number = 1): T[] {
  const result = []

  const { children, ...args } = item
  // debugger
  result.push({
    ...args,
    level,
  })
  // console.log(result)
  if (Array.isArray(children) && children.length) {
    for (let idx = 0; idx < children.length; idx++) {
      const element = children[idx]
      const res = flatTree(element, level + 1)
      result.push(...res)
    }
  }

  return result
}

export const getters: GetterTree<ISystemState, IRootState> = {
  departments: (state) => state.departments,
  roles: (state) => state.roles,
  rights: (state) => state.rights,
  dataRights: (state) => state.dataRights,
  flatDepartments: (state) => {
    return state.departments.reduce((acc, cur) => {
      acc.push(...flatTree(cur))
      return acc
    }, [])
  },
}
